<template>
	<view>
		<!-- 搜索组件 -->
		<!-- 使用view容器组件包裹自定义组件-->
		<view class="search-box">
			<!-- 调用自定义组件-->
			<my-search @click="gotoSearch"></my-search>
		</view>
		
		
		<!-- 轮播图的区域 -->
		<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :circular="true">
			<swiper-item v-for="(item, i) in swiperList" :key="i">
												<!-- 跳转页面并传递goods_id值 -->
				<navigator class="swiper-item" :url="'/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id">
				<!-- <navigator class="swiper-item" > -->
					<image :src="item.image_src"></image>
				</navigator>
			</swiper-item>
		</swiper>
		
		<!-- 分类导航区域 -->
		<view class="nav-list">
			<view class="nav-item" v-for="(item, i) in navList" :key="i" @click="navClickHandler(item)">
				<image :src="item.image_src" class="nav-img"></image>
			</view>
		</view>
		
		<!-- 九宫格区域 -->
		<!-- 包裹性容器 -->
		<view class="grid-list">
			<!-- 九宫格的每一项 shoplist页面是宫格的商品页面-->
			<view class="grid-item" v-for="(item, i) in gridList" :key="i" >
				<navigator :url="'/subpkg/shoplist/shoplist?id=' + item.id">
					<image :src="item.icon"></image>
					<text>{{item.name}}</text>
				</navigator>
			</view>
		</view>
		
		<!-- 楼层区域 -->
		<!-- 楼层的容器 -->
		<view class="floor-list">
			
			<!-- 每一个楼层的 item 项 -->
			<view class="floor-item" v-for="(item, i) in floorList" :key="i">
				
				<!-- 楼层的标题 -->
				<image :src="item.floor_title.image_src" class="floor-title"></image>
				
				<!-- 楼层的图片区域 -->
				<view class="floor-img-box">
					
					<!-- 左侧大图片的盒子 -->
					<navigator class="left-img-box" :url="item.product_list[0].url">
						
						<!-- 索引值为0的图片                           动态绑定样式                                                     宽度不变 高度自适应 -->
						<image :src="item.product_list[0].image_src" :style="{width: item.product_list[0].image_width + 'rpx'}" mode="widthFix"></image>
					</navigator>
					
					<!-- 右侧 4 个小图片的盒子 -->
					<view class="right-img-box">                                                             <!-- 第0张图片依然显示？？？ -->
						<navigator class="right-img-item" v-for="(item2, i2) in item.product_list" :key="i2" v-if="i2 !== 0" :url="item2.url">
							<image :src="item2.image_src" :style="{width: item2.image_width + 'rpx'}" mode="widthFix"></image>
						</navigator>
					</view>
					
				</view>
			</view>
		</view>
			
	</view>
</template>

<script>
	import badgeMix from '/mixins/tabbar-badge.js'
	
	export default {
		mixins: [badgeMix],
		
		data() {
			return {
				// 这是轮播图的数据列表
				swiperList: [
					{
						"goods_id": 1,
						"image_src":"/static/images/SwiperPicture/1.jpg"
					},
					{
						"goods_id": 2,
						"image_src":"/static/images/SwiperPicture/2.jpg"
					},
					{
						"goods_id": 3,
						"image_src":"/static/images/SwiperPicture/3.jpg"
					},
					{
						"goods_id": 4,
						"image_src":"/static/images/SwiperPicture/4.jpg"
					},
					{
						"goods_id": 5,
						"image_src":"/static/images/SwiperPicture/5.jpg"
					},
				],
				
				
				gridList: [
					{
						"id":1,
						"icon":"/static/images/gridPicture/体育.png",
						"name":"体育"
					},
					{
						"id":2,
						"icon":"/static/images/gridPicture/动漫.png",
						"name":"动漫"
					},
					{
						"id":3,
						"icon":"/static/images/gridPicture/动物园.png",
						"name":"动物"
					},
					{
						"id":4,
						"icon":"/static/images/gridPicture/摄影.png",
						"name":"摄影"
					},
					{
						"id":5,
						"icon":"/static/images/gridPicture/汽车.png",
						"name":"汽车"
					},
					{
						"id":6,
						"icon":"/static/images/gridPicture/游戏.png",
						"name":"游戏"
					},
					{
						"id":7,
						"icon":"/static/images/gridPicture/电脑.png",
						"name":"电脑"
					},
					{
						"id":8,
						"icon":"/static/images/gridPicture/美食.png",
						"name":"美食"
					},
					{
						"id":9,
						"icon":"/static/images/gridPicture/音乐.png",
						"name":"音乐"
					},
					{
						"id":10,
						"icon":"/static/images/gridPicture/手机.png",
						"name":"手机"
					},
					{
						"id":11,
						"icon":"/static/images/gridPicture/教育.png",
						"name":"教育"
					},
					{
						"id":12,
						"icon":"/static/images/gridPicture/电影.png",
						"name":"电影"
					}	
					    
				],
				
				// 分类导航数据列表
				navList:[],
				
				// 1. 楼层的数据
				floorList: []
			};
		},
		
		onLoad() {
			// 调用方法，获取轮播图的数据
			this.getSwiperList()
			
			// 调用方法，获取宫格的数据
			//this.getGridList()
			
			// 获取导航栏数据的方法/函数
			this.getNavList()
			
			//this.$uni.$showMsg()
			
			// 获取楼层数据方法/函数定义,调用该方法
			this.getFloorList()
		},
		
		methods:{
			async getSwiperList() {
				// 解构data，重命名为res
				const {data:res} = await uni.$http.get('/api/public/v1/home/swiperdata')
				
				console.log(res)
				
				
				// 请求失败
				if (res.meta.status !== 200) {
					//封装uni.$showMsg()函数,此处需要封装
					return uni.$showMsg()
				}
				
				this.swiperList = res.message
				//uni.$showMsg('数据请求成功!')  //报错，common_vendor.index.$showMsg is not a function

			},
			
			// async gridList() {
			// 	// 解构data，重命名为res
			// 	const {data:res} = await uni.$http.get('/api/public/v1/home/griddata')
				
			// 	console.log(res)
				
				
			// 	// 请求失败
			// 	if (res.meta.status !== 200) {
			// 		//封装uni.$showMsg()函数,此处需要封装
			// 		return uni.$showMsg()
			// 	}
				
			// 	this.gridList = res.message
			// 	//uni.$showMsg('数据请求成功!')  //报错，common_vendor.index.$showMsg is not a function
			
			// },
			
			// 获取导航栏数据的方法/函数实现的详细代码
			async getNavList() {
				// 解构data，重命名为res
				const {data:res} = await uni.$http.get('/api/public/v1/home/catitems')
				
				console.log(res)
				
				// 请求失败
				if (res.meta.status !== 200) {
					//封装uni.$showMsg()函数,此处需要封装
					return uni.$showMsg()
				}
				
				this.navList = res.message
				//uni.$showMsg('数据请求成功!')  //报错，common_vendor.index.$showMsg is not a function
				
			},
			
			//分类图标点击后页面跳转
			navClickHandler(item) {
				console.log(item)
				if (item.name === '分类') {
					uni.switchTab({
						url: '/pages/cate/cate'
					})
				}
			},
			
			// 获取楼层数据方法/函数的实现
			async getFloorList() {
				// 解构data，重命名为res
				const {data:res} = await uni.$http.get('/api/public/v1/home/floordata')
				
				console.log(res)
				
				// 请求失败
				if (res.meta.status !== 200) {
					//封装uni.$showMsg()函数,此处需要封装
					return uni.$showMsg()
				}
				
				//this.floorList = res.message
				//uni.$showMsg('数据请求成功!')  //报错，common_vendor.index.$showMsg is not a function
				
				// 对数据进行处理
				// 对message信息进行一重循环遍历，找到floor的数据
				res.message.forEach(floor => {
					//对floor数据进行一重循环便利，找到product_list数据
					floor.product_list.forEach(prod => {
						prod.url = '/subpkg/goods_list/goods_list?' + prod.navigator_url.split('?')[1]
					})
				})
				this.floorList = res.message
				
			},
			
			// 调用自定义组件的自定义事件函数
			gotoSearch(){
				uni.navigateTo({
					url: '/subpkg/search/search'
				})
			}
				
		}
	}
</script>

<style lang="scss">
	swiper {
	    height: 350rpx;
	
	    .swiper-item,
	    image {
			width: 100%;
			height: 100%;
	    }
	}
	
	.nav-list {
	    display: flex;
	    justify-content: space-around;
	    margin: 15px 0;
	
	    .nav-img {
	      width: 128rpx;
	      height: 120rpx;
	    }
	}
	
	.floor-title {
	    width: 100%;
	    height: 60rpx;
	}
	
	.floor-img-box {
	    display: flex;
	    padding-left: 10rpx;
		justify-content: space-around;
		
		.left-img-box {
		    display: flex;
		    flex-wrap: wrap;
		    justify-content: space-around;
		}
		
		.right-img-box {
		    display: flex;
		    flex-wrap: wrap;
		    justify-content: space-around;
			align-items: end;
		}
	}
	
	//实现吸顶效果的样式设计 
	.search-box {
		// 设置定位效果为"吸顶"
	    position: sticky;
		// 吸顶的位置
	    top: 0;
		// 提高层级，防止被轮播图覆盖
	    z-index: 999;
	}
	
	/* 九宫格大方框 */
	.grid-list{
	  display: flex;  /* 灵活布局 */
	  flex-wrap: wrap;  /* 一行放不下，允许换行 */
	  border-left: 1rpx solid #efefef;  /* 左侧加边框 */
	  border-top: 1rpx solid #efefef;  /* 上侧加边框 */
	}
	
	/* 九宫格中的每一项 */
	.grid-item{
	  width: 25%;
	  height: 180rpx;
	  display: flex;  /* 灵活布局 */
	  flex-direction: column;  /* 纵向布局 */
	  align-items: center;  /* 横向居中 */
	  justify-content: center;  /* 纵向居中 */
	  border-right: 1rpx solid #efefef;  /* 右侧加边框 */
	  border-bottom: 1rpx solid #efefef;  /* 下侧加边框 */
	  box-sizing: border-box;  /* 表示内边距padding、边框border被包含在设置的宽度里面 */
	}
	
	.grid-item image{
	  width: 60rpx;
	  height: 60rpx;
	}
	
	.grid-item text{
	  font-size: 25rpx;
	  margin-top: 5rpx;  /* 文本和图片的上边间距 */
	}
	
	.img-box{
	  display: flex;  /* 两张图片放置在一行里 */
	  padding: 20rpx 10rpx;
	  justify-content: space-around;
	
	}
	
	.img-box image{
	  width: 45%;
	  height: 100%;
	}

</style>
